/**
 * 
 */
package com.anduinslegacy.armory;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import com.anduinslegacy.datastore.PMF;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * @author Stephen Jennings
 *
 */
public class CharacterFactory {
	public static void create(String _name, int _points) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		
		try {
			tx.begin();

			Key k = KeyFactory.createKey("Character", _name.toLowerCase()); 
            Character character;
            
            try { 
            	character = pm.getObjectById(Character.class, k);                
            } catch (JDOObjectNotFoundException e) {            
            	character = new Character(_name.toLowerCase(), _points);            
            } 
            
            character.setPoints(_points); 
            character.setKey(k);
            
            pm.makePersistent(character); 

			tx.commit();
		} finally {
			if (tx.isActive()) { 
                tx.rollback(); 
            }
		}
	}

}
